package controller

import (
	"fmt"
	"strconv"
	"time"

	"gitee.com/liu_deping/gin-vue/dao"
	"gitee.com/liu_deping/gin-vue/db"
	"gitee.com/liu_deping/gin-vue/model"
	"github.com/gin-gonic/gin"
	"github.com/jinzhu/gorm"
	_ "github.com/jinzhu/gorm/dialects/mysql"
	uuid "github.com/satori/go.uuid"
)

type PostController struct { //让结构体实现接口
	DB gorm.DB
	RestController
}

func NewPostController() PostController {
	return PostController{
		DB: *db.DB.AutoMigrate(&model.Post{}),
	}
}

func (p *PostController) Add(ctx *gin.Context) {
	//获取参数
	userID_str := ctx.PostForm("user_id")
	categoryID_str := ctx.PostForm("category_id")
	title := ctx.PostForm("title")
	head_img := ctx.PostForm("head_img")
	content := ctx.PostForm("content")
	//通过前端获得的用户ID为string类型，需要转化为数值型
	userID, _ := strconv.Atoi(userID_str)
	categoryID, _ := strconv.Atoi(categoryID_str)
	//创建Post对象
	post := &model.Post{
		ID:         uuid.NewV4(), //随机生成uuid;
		UserId:     uint(userID), //
		CategoryId: uint(categoryID),
		Title:      title,
		HeadImg:    head_img,
		Content:    content,
		CreatedAt:  time.Now(), //创建时间
		UpdateAt:   time.Now(), //修改时间
	}
	//添加该条数据
	dao.AddPost(post)
}

// func (p *PostController) Update(ctx *gin.Context) {
// 	//获取参数
// 	//1.获取数据
// 	userID_str := ctx.PostForm("user_id")
// 	categoryID_str := ctx.PostForm("category_id")
// 	title := ctx.PostForm("title")
// 	head_img := ctx.PostForm("head_img")
// 	content := ctx.PostForm("content")
// 	if userID_str != "" {
// 		post := &model.Post{}
// 		db.DB.Where("user_id=?", userID_str).First(post)
// 		if post.UserId!="" {

// 		}
// 	}
// 	//响应前端
// 	response.Success(ctx, gin.H{"category": "?"}, "修改分类成功")
// }

func (p *PostController) GetAll(ctx *gin.Context) {
	//获取参数
	title := ctx.PostForm("title")
	fmt.Println(title)
}

func (p *PostController) DeleteByID(ctx *gin.Context) {
	//获取参数
	title := ctx.PostForm("title")
	fmt.Println(title)
}
